package com.example.boos.ui.dashboard;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.baidu.mapapi.model.LatLng;
import com.example.boos.HttpUtil.MyPost;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.orderDetail.OrderDetailActivity;
import com.example.boos.pojo.Food;
import com.example.boos.pojo.Orders;
import com.example.boos.util.MyDistanceUtil;
import com.example.boos.util.MyThreadPool;

import java.util.ArrayList;
import java.util.List;

public class OrderItemDashAdapter extends BaseAdapter {

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 2:
                    // 跳转到订单详细页面
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(context, OrderDetailActivity.class));
                    intent.putExtras((Bundle)msg.obj);
                    context.startActivity(intent);
                    break;
            }
        }
    };

    private Context context;

    private static List<Orders> data = new ArrayList<>();

    public OrderItemDashAdapter(List<Orders> data, Context context){
        this.data = data;
        this.context = context;
    }

    public void updateAll(List<Orders> data){
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return data == null ? 0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if(convertView == null){
            convertView = View.inflate(parent.getContext(), R.layout.order_item_dashboard,null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }

        // 设置数据
        Orders order = data.get(position);
        // 根据订单的状态来设置
        if(order.getRiderStatus() == 0){
            // 骑手还未接单
            vh.orderStatus.setText("等待骑手接单");
        }else if(order.getRiderStatus() == 1){
            // 骑手已接单，还未取货
            vh.orderStatus.setText("等待骑手取货");
        }else if(order.getRiderStatus() == 2){
            // 骑手已取货，还未配送
            vh.orderStatus.setText("骑手正在配送中");
        }else{
            vh.orderStatus.setText("等待用户确认收货");
        }

        vh.noti_orderStartTime.setText(order.getStartTime().toString());
        vh.noti_remark.setText(order.getRemark());
        vh.noti_userAddrss.setText(order.getUserAddress().getAddress());
        vh.noti_totalPrice.setText(String.valueOf(order.getTotalPrice()));
        vh.noti_orderId.setText(String.valueOf(order.getId()));

        LatLng other = new LatLng(Double.parseDouble(order.getUserAddress().getLatitude()),
                Double.parseDouble(order.getUserAddress().getLongitude()));
        String distance = MyDistanceUtil.formatDistance(BossInfo.cur,other);
        vh.noti_distance.setText(distance);
        // 查看详细订单  按钮 监听函数
        vh.noti_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取到 order
                // id - num
                String[] split = order.getFoodIdList().split(",");
                List<Food> foods = new ArrayList<>();
                MyThreadPool.doJob(new Runnable() {
                    @Override
                    public void run() {
                        for(int i =0;i<split.length;i++){
                            String[] temp = split[i].split("-");
                            Food food = new Food();
                            food = (Food) MyPost.doPost("bossgetFood",Integer.valueOf(temp[0]));
                            food.setAddNums(Integer.valueOf(temp[1]));
                            foods.add(food);
                        }
                        order.set_foodList(foods);
                        // 跳转到详细页面
                        Message msg = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("order",order);
                        msg.obj = bundle;
                        msg.what = 2;
                        handler.sendMessage(msg);
                    }
                });

            }
        });



        // 取消
        vh.noti_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;
    }

    class  ViewHolder{
        TextView orderStatus;
        TextView noti_orderStartTime;
        TextView noti_distance;
        TextView noti_userAddrss;
        TextView noti_remark;
        TextView noti_totalPrice;
        TextView noti_orderId;
        Button noti_cancel;
        Button noti_detail;
        public ViewHolder(View view){
            noti_orderId = view.findViewById(R.id.noti_orderId);
            noti_orderStartTime = view.findViewById(R.id.noti_orderStartTime);
            noti_distance = view.findViewById(R.id.noti_distance);
            noti_userAddrss = view.findViewById(R.id.noti_userAddrss);
            noti_remark = view.findViewById(R.id.noti_remark);
            noti_totalPrice = view.findViewById(R.id.noti_totalPrice);
            noti_cancel = view.findViewById(R.id.noti_cancel);
            noti_detail = view.findViewById(R.id.noti_detail);
            orderStatus = view.findViewById(R.id.dash_orderStatus);
        }
    }
}
